package com.ciaojian.store.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.mapper.WorkerMapper;
import com.ciaojian.core.model.Roster;
import com.ciaojian.core.model.Worker;
import com.ciaojian.core.service.WorkerService;
import com.ciaojian.core.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Atlakyn
 */
@Service
public class WorkerServiceImpl extends WorkerService<WorkerMapper, Worker> implements IService<Worker> {
    @Resource
    private OrderMapper orderMapper;

    /**
     * 发型师数据列表
     *
     * @param storeId   门店 id
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param page      分页插件
     * @return 发型师数据列表
     */
    public Map<String, Object> findWorkerData(Integer storeId,
                                              Date startDate,
                                              Date endDate,
                                              List<Integer> orderTypes,
                                              Page<Worker> page) {
        LambdaQueryWrapper<Worker> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // storeId = 0代表查全部
        lambdaQueryWrapper.eq(storeId != 0, Worker::getStoreId, storeId)
                .ne(Worker::getStatus, 8)
                .select(Worker::getId,
                        Worker::getName,
                        Worker::getJoinTime,
                        Worker::getLevelId,
                        Worker::getType,
                        Worker::getAvatar
                );
        IPage<Worker> pageWorker = this.page(page, lambdaQueryWrapper);

        List<Map<String, Object>> list = new ArrayList<>();
        pageWorker.getRecords().forEach(i -> {
            Map<String, Object> data = this.baseMapper.findWorkerData(i.getId(), startDate, endDate, storeId, orderTypes);
            putMap(storeId, startDate, endDate, list, i, data);
        });

        Map<String, Object> result = new HashMap<>(3);
        result.put("records", list);
        result.put("current", pageWorker.getCurrent());
        result.put("pages", pageWorker.getPages());
        return result;
    }

    /**
     * 发型师数据列表
     *
     * @param storeId   门店 id
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param page      分页插件
     * @return 发型师数据列表
     */
    public Map<String, Object> findMemberOrderData(Integer storeId,
                                                   Date startDate,
                                                   Date endDate,
                                                   Page<Worker> page) {
        // storeId = 0代表查全部
        IPage<Worker> pageWorker = lambdaQuery().eq(storeId != 0, Worker::getStoreId, storeId)
                .ne(Worker::getStatus, 8)
                .select(Worker::getId,
                        Worker::getName,
                        Worker::getJoinTime,
                        Worker::getLevelId,
                        Worker::getType,
                        Worker::getAvatar
                ).page(page);

        List<Map<String, Object>> list = new ArrayList<>();
        pageWorker.getRecords().forEach(i -> {
            Map<Integer, Map<String, Object>> data = this.baseMapper.findMemberOrderData(i.getId(), startDate, endDate, storeId);
            data.values().forEach(j->{
                putMap(storeId, startDate, endDate, list, i, j);
            });
        });

        Map<String, Object> result = new HashMap<>(3);
        result.put("records", list);
        result.put("current", pageWorker.getCurrent());
        result.put("pages", pageWorker.getPages());
        return result;
    }

    /**
     * 公共方法
     */
    private void putMap(Integer storeId, Date startDate, Date endDate, List<Map<String, Object>> list, Worker i, Map<String, Object> j) {
        j.put("name", i.getName());
        j.put("joinTime", i.getJoinTime());
        j.put("level", i.getLevelId());
        j.put("type", i.getType());
        j.put("avatar", i.getAvatar());
        j.put("id", i.getId());
        j.putAll(orderMapper.countCustomerByWorkerId(i.getId(), startDate, endDate, storeId));
        list.add(j);
    }

    public Result findDRosterList(String storeId) {
        List<Roster> dRosterList = baseMapper.findDRosterList(storeId);
        return Result.ok(dRosterList);
    }
}
